package com.platform.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.enumstatus.UserEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.Query;
import com.platform.modules.sys.dao.SysOrgUserDao;
import com.platform.modules.sys.entity.SysOrgUserEntity;
import com.platform.modules.sys.entity.SysUserRoleEntity;
import com.platform.modules.sys.service.SysOrgUserService;
import com.platform.modules.sys.service.SysRoleService;
import com.platform.modules.sys.service.SysUserRoleService;

/**
 * @author wangLc
 * 注册机构人员信息
 * 2019-5-18
 */
@Service("sysOrgUserService")
public class SysOrgUserServiceImpl extends ServiceImpl<SysOrgUserDao, SysOrgUserEntity> implements SysOrgUserService {
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleService sysRoleService;

	@Override
	public List<SysOrgUserEntity> queryAll(Map<String, Object> params) {
		return baseMapper.selectListPage(params);
	}

	@Override
	public Page queryPage(Map<String, Object> params) {
		// 排序
		params.put("sidx", "T.CREATE_TIME");
		params.put("asc", false);
		Page<SysOrgUserEntity> page = new Query<SysOrgUserEntity>(params).getPage();
		List<SysOrgUserEntity> orgUserList = baseMapper.selectListPage(page, params);
		return page.setRecords(orgUserList);
	}

	
     /**
      * 保存用户与单位的关系
      */
	 public void updateOrgUser(String userId, List<String> orgNoList){
		Map<String, Object> map = new HashMap<>(2);
		map.put("user_id", userId);
		// 先删除用户与角色关系
		this.removeByMap(map);

		if (orgNoList == null || orgNoList.size() == 0) {
			return;
		}

		// 保存用户与角色关系
		List<SysOrgUserEntity> list = new ArrayList<>(orgNoList.size());
		for (String orgNo : orgNoList) {
			SysOrgUserEntity sysOrgUserEntity = new SysOrgUserEntity();
			sysOrgUserEntity.setUserId(userId);
			sysOrgUserEntity.setOrgNo(orgNo);
			list.add(sysOrgUserEntity);
		}
		this.saveBatch(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(SysOrgUserEntity user, Map<String, Object> params) {
		user.setCreateTime(new Date());
		this.save(user);

		// 检查角色是否越权
		checkRole(user, params);

		// 保存用户与角色关系
//		sysUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SysOrgUserEntity user, Map<String, Object> params) {

		this.updateById(user);

		// 检查角色是否越权
		checkRole(user, params);

		// 保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String[] userIds) {
		this.removeByIds(Arrays.asList(userIds));
	}

	@Override
	public boolean updatePassword(String userId, String password, String newPassword) {
		SysOrgUserEntity userEntity = new SysOrgUserEntity();
		return this.update(userEntity,
				new QueryWrapper<SysOrgUserEntity>().eq("user_id", userId).eq("password", password));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void resetPw(String[] userIds) {
		for (int i = 0; i < userIds.length; i++) {
			SysOrgUserEntity user = this.getById(userIds[i]);
			this.updateById(user);
		}
	}

	/**
	 * 检查角色是否越权
	 */
	private void checkRole(SysOrgUserEntity user, Map<String, Object> params) {
		if (user.getRoleIdList() == null || user.getRoleIdList().size() == 0) {
			return;
		}

		// 查询用户权限下的角色列表
		List<String> roleIdList = sysRoleService.queryRoleIdList(params);

		// 判断是否越权
		if (!roleIdList.containsAll(user.getRoleIdList())) {
			throw new BusinessException("新增用户所选角色，不是本人创建");
		}
	}

	
	public List<String> selectProcessUser(String orgNo,String[] arr){
		
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("orgNo", orgNo);
		if(arr !=null && arr.length>0){
			params.put("processCodes", arr);
		}
		return baseMapper.selectProcessUser(params);
	}

}
